Skip to content

fix: coordinate mismatch in EnsureCursorVisible() causing 4-pixel scroll margin#172

Open
Bert0ns wants to merge 3 commits into
BalazsJako:devfrom
Bert0ns:dev
Open

fix: coordinate mismatch in EnsureCursorVisible() causing 4-pixel scroll margin#172
Bert0ns wants to merge 3 commits into
BalazsJako:devfrom
Bert0ns:dev

Conversation

@Bert0ns

@Bert0ns Bert0ns commented Jul 8, 2026

Copy link
Copy Markdown

Issue: in TextEditor::EnsureCursorVisible() where typing near the horizontal edges of the window would cause the view to scroll by exactly 1 character per keystroke, effectively pinning the cursor to the physical edge of the editor view.

The Bug

The previous implementation mixed up character column indices and physical pixel floats when attempting to calculate padding bounds:

  1. len (from TextDistanceToLineStart) correctly returned physical pixels (float).
  2. left and right were calculated by dividing scroll dimensions by mCharAdvance.x, returning character columns (int).
  3. The comparison len + mTextStart < left + 4 thus incorrectly compared pixels directly to character columns.
  4. Furthermore, ImGui::SetScrollX() expects a pixel value. By calling ImGui::SetScrollX(... - 4), the logic intended to subtract a margin of 4 characters, but mathematically subtracted exactly 4 physical pixels instead.

The Fix

This PR fully normalizes the EnsureCursorVisible logic to use consistent, floating-point pixel math for all boundary and padding calculations.

  • It introduces proper pixel-aware horizontal padding (padX = 4 characters).
  • It removes arbitrary vertical padding, allowing the cursor to rest naturally at the top/bottom edges of the window without forcing unnecessary vertical scrolling.
  • It fixes a bug where ImGui::Dummy (which defines the maximum horizontal scroll bounds) did not account for padding. By adding the horizontal padding to the Dummy width, it ensures that padding is maintained even when typing on the longest line of the document.
  • It bounds the padding dynamically via std::min(..., width * 0.25f) to ensure that if the editor window is resized to be extremely small, the padding doesn't mathematically overlap and cause the scroll position to oscillate.

Testing

  • Verified on a standalone integration: Typing continuously towards the right margin now smoothly scrolls the view while maintaining a comfortable, properly calculated 4-character margin ahead of the cursor.
  • Verified that horizontal padding is correctly applied even on the absolute longest line of the document.
  • Ensured no new ImGui internals are called and no memory is allocated.

Copilot AI review requested due to automatic review settings July 8, 2026 00:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants